package cn.maihe.elg.operation.schedule;

import cn.maihe.elg.operation.config.CiticBankConfig;
import cn.maihe.elg.operation.supports.bank.dto.CiticAccountDetailRespDTO;
import cn.maihe.elg.operation.supports.bank.service.CiticBankAccountDetailService;
import cn.maihe.elg.operation.utils.ElgLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @description: 中信银行账户明细查询定时任务
 */
@Component
@Slf4j
public class CiticBankAccountDetailSchedule {

    @Resource
    private CiticBankAccountDetailService citicBankAccountDetailService;
    
    @Resource
    private CiticBankConfig citicBankConfig;

    /**
     * 定时查询中信银行账户明细并处理
     * 默认每分钟执行一次，可通过配置文件修改
     */
    @Scheduled(cron = "${cn.maihe.elg.bank.citic-bank.accountDetailCron:0 0/1 * * * ?}")
    public void queryAndProcessAccountDetails() {
        if (!"1".equals(citicBankConfig.getOpenFlag())) {
            ElgLogger.info(log, l -> l.info("中信银行功能未开启，跳过定时任务。"));
            return;
        }

        try {
            ElgLogger.info(log, l -> l.info("开始执行中信银行账户明细查询定时任务。"));
            String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));

            //查询当日全部流水
            CiticAccountDetailRespDTO response = citicBankAccountDetailService.queryFullTransactionsByDate(today, today);

            //处理查询到的流水
            if (response != null && "AAAAAAA".equals(response.getStatus())) {
                ElgLogger.info(log, l -> l.info("中信银行当日流水查询成功，共获取到 {} 条记录。", response.getReturnRecords()));
                citicBankAccountDetailService.processAccountDetails(response);
                ElgLogger.info(log, l -> l.info("中信银行账户明细处理完成。"));
            } else {
                String errorMsg = response != null ? response.getStatusText() : "响应为空或状态码非成功";
                ElgLogger.warn(log, l -> l.warn("查询当日流水失败或无数据: {}", errorMsg));
            }

        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("中信银行账户明细查询定时任务执行异常: {}", e.getMessage(), e));
        }
    }

    /**
     * 手动触发账户明细查询
     */
    public void manualQueryAccountDetails() {
        try {
            ElgLogger.info(log, l -> l.info("手动触发中信银行账户明细查询。"));
            String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            CiticAccountDetailRespDTO response = citicBankAccountDetailService.queryFullTransactionsByDate(today, today);

            if (response != null && "AAAAAAA".equals(response.getStatus())) {
                ElgLogger.info(log, l -> l.info("手动查询成功，共获取到 {} 条记录。", response.getReturnRecords()));
                citicBankAccountDetailService.processAccountDetails(response);
                ElgLogger.info(log, l -> l.info("手动查询明细处理完成。"));
            } else {
                String errorMsg = response != null ? response.getStatusText() : "响应为空或状态码非成功";
                ElgLogger.warn(log, l -> l.warn("手动查询失败或无数据: {}", errorMsg));
                throw new RuntimeException("手动查询失败: " + errorMsg);
            }
        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("手动查询中信银行账户明细异常: {}", e.getMessage(), e));
            throw new RuntimeException("手动查询中信银行账户明细异常", e);
        }
    }
}